﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_frmProductCategoryView : System.Web.UI.Page
{
    clsUser clsUser;
    clsProductCategoryList clsProductCategoryList;

    #region EVENT MODULES

    protected void Page_Load(object sender, EventArgs e)
    {
        //### Check if session clsUser exists
        if (Session["clsUser"] == null)
        {
            Response.Redirect("../frmAdminLogin.aspx");
        }

        clsUser = (clsUser)Session["clsUser"];

        if (!IsPostBack)
        {
            //### If delete item is selected
            if (Request.QueryString["action"] == "delete")
            {
                DeleteItem(Convert.ToInt16(Request.QueryString["iProductCategoryID"]));
            }

            clsProductCategoryList = new clsProductCategoryList(clsUser);
            Session["clsCategoryProductsList"] = clsProductCategoryList;
            GetRecords();
        }
        else
        {
            clsProductCategoryList = (clsProductCategoryList)Session["clsProductCategoryList"];
        }
    }

    protected void btnSearch_Click(object sender, ImageClickEventArgs e)
    {
        //### Filter according to search criteria
        try
        {
            dgrGrid.CurrentPageIndex = 0;

            //### Reset session variable
            Session["CategoryProductsView_CurrentPageIndex"] = null;

            Session["clsProductCategoryList"] = clsProductCategoryList;
            GetRecords();

        }
        catch (Exception ex)
        {
            //*** clsErrorLog.LogError(ex, this, clsUser, 95);
            //*** Response.Write(clsCommonFunctions.JSAlert("Invalid Search Criteria."));
        }
    }

    protected void btnAdd_Click(object sender, ImageClickEventArgs e)
    {
        //### Redirect to add / modify user page
        Response.Redirect("frmProductCategoryAddModify.aspx");
    }

    #endregion

    #region POPULATE DATA METHODS

    private void GetRecords()
    {
        //### Populate datagrid using clsCategoriesList object
        try
        {
            Session["clsProductCategoryList"] = clsProductCategoryList;

            string strSearchCriteria = "";

            clsProductCategoryList.FilterExpression = strSearchCriteria;

            dgrGrid.DataSource = clsProductCategoryList.GetRecords();

            //### Add this check to trap if the last item on the page is deleted
            if (Convert.ToInt16(dgrGrid.CurrentPageIndex) < Convert.ToInt16(Session["CategoryProductsView_CurrentPageIndex"]))
            {
                dgrGrid.CurrentPageIndex = 0;
            }
            else
            {
                if (Session["CategoryProductsView_CurrentPageIndex"] != null)
                {
                    dgrGrid.CurrentPageIndex = (int)Session["CategoryProductsView_CurrentPageIndex"];
                }
            }

            dgrGrid.DataBind();

        }
        catch (Exception ex)
        {
            clsErrorLog.LogError(ex, this, clsUser, 95);
            Response.Write(clsCommonFunctions.JSAlert(ex.Message.ToString()));
        }
    }

    #endregion

    #region DATA GRID METHODS

    protected void dgrGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        //### Method is used when the page index is changed.
        try
        {
            Session["CategoryProductsView_CurrentPageIndex"] = e.NewPageIndex;
            dgrGrid.CurrentPageIndex = e.NewPageIndex;
            GetRecords();
        }
        catch (Exception ex)
        {
            clsErrorLog.LogError(ex, this, clsUser, 95);
            Response.Write(clsCommonFunctions.JSAlert(ex.Message.ToString()));
        }
    }

    protected void dgrGrid_SortCommand(object source, DataGridSortCommandEventArgs e)
    {
        //### Method is used when sorting.
        try
        {
            if (clsProductCategoryList.SortExpression == e.SortExpression)
                clsProductCategoryList.SortExpression = e.SortExpression + " DESC";
            else
                clsProductCategoryList.SortExpression = e.SortExpression;
            Session["clsProductCategoryList"] = clsProductCategoryList;
            GetRecords();
        }
        catch (Exception ex)
        {
            //*** clsErrorLog.LogError(ex, this, clsUser, 95);
            //*** Response.Write(clsCommonFunctions.JSAlert(ex.Message.ToString()));
        }
    }

    public void dgrGrid_PopEditLink(object sender, EventArgs e)
    {
        try
        {
            //### Add hyperlink to datagrid item
            HyperLink lnkEditItem = (HyperLink)sender;
            DataGridItem dgrItemEdit = (DataGridItem)lnkEditItem.Parent.Parent;

            //### Get the iCategoryID
            int iProductCategoryID = 0;
            iProductCategoryID = int.Parse(dgrItemEdit.Cells[0].Text);

            //### Add attributes to edit link
            lnkEditItem.CssClass = "dgrEditLink";
            lnkEditItem.Attributes.Add("href", "frmProductCategoryAddModify.aspx?iProductCategoryID=" + iProductCategoryID);
        }
        catch (Exception ex)
        {
            clsErrorLog.LogError(ex, this, clsUser, 95);
            Response.Write(clsCommonFunctions.JSAlert(ex.Message.ToString()));
        }
    }

    public void dgrGrid_PopDeleteLink(object sender, EventArgs e)
    {
        try
        {
            //### Add hyperlink to datagrid item
            HyperLink lnkDeleteItem = (HyperLink)sender;
            DataGridItem dgrItemDelete = (DataGridItem)lnkDeleteItem.Parent.Parent;

            //### Get the iCategoryID
            int iProductCategoryID = 0;
            iProductCategoryID = int.Parse(dgrItemDelete.Cells[0].Text);

            //### Add attributes to delete link
            lnkDeleteItem.CssClass = "dgrDeleteLink";
            lnkDeleteItem.Attributes.Add("href", "frmProductCategoryView.aspx?action=delete&iProductCategoryID=" + iProductCategoryID);

        }
        catch (Exception ex)
        {
            clsErrorLog.LogError(ex, this, clsUser, 95);
            Response.Write(clsCommonFunctions.JSAlert(ex.Message.ToString()));
        }
    }

    #endregion

    #region DELETE DATA METHODS METHODS

    private void DeleteItem(int iProductID)
    {
        //### We instanciate the record object here.
        clsProductsRecord clsProductsRecord = new clsProductsRecord(clsUser, iProductID);

        //### Call the delete function
        clsProductsRecord.Delete();
    }

    #endregion
}